package com.hns.tool.pub;

import java.io.File;
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  

import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.transform.Transformer;  
import javax.xml.transform.TransformerConfigurationException;  
import javax.xml.transform.TransformerException;  
import javax.xml.transform.TransformerFactory;  
import javax.xml.transform.dom.DOMSource;  
import javax.xml.transform.stream.StreamResult;  

import org.w3c.dom.Document;  
import org.w3c.dom.Element;  
import org.w3c.dom.Node;  
import org.xml.sax.SAXException; 


/**
 * 
* @ClassName: SAXUtil 
* @Description: 
* @author zhangl
* @email victor_007@126.com
* @date 2016年11月18日 下午3:26:26
 */
public class SAXUtil {

    public static String plistPath =  SAXUtil.class.getClassLoader().getResource("HNS_SC_Cloud_Config.plist").getPath().replaceAll("%20", " ") ;
    /**
     * 
     * @Title:  writePlist
     * @Description: 手机版本升级时改写plist文件，下载路径与文件名同步
     * @param file
     * void
     */
    public static File writePlist(String index, int count, String value, boolean outOrno){
        Document document = null;  
        File file = null;
        try {  
            file = new File(plistPath);
            document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);  
            Element root = document.getDocumentElement();  
            
            root.getElementsByTagName(index).item(count).setTextContent(value);  
            output(root, file);  
        } catch (SAXException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (ParserConfigurationException e) {  
            e.printStackTrace();  
        }  
        return file;
    }
    
    public static void output(Node node, File file) {  
        TransformerFactory transFactory = TransformerFactory.newInstance();  
        try {  
              Transformer transformer = transFactory.newTransformer();  
              // 设置各种输出属性  
              transformer.setOutputProperty("encoding", "utf-8");  
              transformer.setOutputProperty("indent", "yes");  
              DOMSource source = new DOMSource();  
              // 将待转换输出节点赋值给DOM源模型的持有者(holder)  
              source.setNode(node);  
              StreamResult result = new StreamResult();  
              if (file == null) {  
                // 设置标准输出流为transformer的底层输出目标  
                result.setOutputStream(System.out);  
              } else {  
                result.setOutputStream(new FileOutputStream(file));  
              }  
              // 执行转换从源模型到控制台输出流  
              transformer.transform(source, result);  
        } catch (TransformerConfigurationException e) {  
            e.printStackTrace();  
        } catch (TransformerException e) {  
            e.printStackTrace();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
    }  
    
    public static void main(String args[]){
        //File file = new File("D://HNS_SC_Cloud_Config.plist");
        SAXUtil.writePlist("string", 1, "http://www.baidu.com", false);
    }
}
